home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Documentation / Tech Notes & Articles / Recipes / Documents / Insert... Menuitem Recipe next >
Encoding:
Text File  |  1995-04-21  |  3.6 KB  |  177 lines  |  [TEXT/ttxt]

  1. OpenDoc™ Recipes
  2.  
  3.  
  4. Insert... Menuitem Recipe
  5. By The OpenDoc Design Team
  6. April 20th, 1995
  7.  
  8.  
  9. © 1993-1995  Apple Computer, Inc. All Rights Reserved.
  10. Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
  11. Mac and OpenDoc are trademarks of Apple Computer, Inc. 
  12.  
  13.  
  14.  
  15. Introduction
  16.  
  17. The fourth item in the OpenDoc “Document” menu is “Insert…” which a user chooses if they want to insert a part from a disk using the Standard File interface.  This recipes serves as sample code to do this. 
  18.  
  19. Caveats
  20.  
  21. • This code does NOT replicate the code for Cloning and Embedding.  Please see the Clipboard recipe for that code.
  22.  
  23. • It does not contain proper error checking
  24.  
  25. • It does not account for the case where a part may want to Insert… a file which is not an OpenDoc part.
  26.  
  27. • It does not properly filter out files which are not ODContainers
  28.  
  29. • Instead of kODDefaultFileContainer, it should use  file->GetContainerType()    
  30.  
  31. • It uses the PlatformFile class which is an unsupported private utility class of OpenDoc
  32.  
  33. • It always embeds the inserted part, whereas some part editors may want to incorporate inserted content.
  34.  
  35. Sample Code
  36.  
  37. #include "ODSessn.xh"
  38.  
  39. #include "WinStat.xh"
  40.  
  41. #include "PlfmFile.h"
  42.  
  43. #include "Storage.h"
  44.  
  45. #include "ODCtr.xh"
  46.  
  47. #include "Document.xh"
  48.  
  49. #include "Draft.xh"
  50.  
  51. #include "StorageU.xh"
  52.  
  53.  
  54.  
  55.  
  56. void CMyPart::Insert(Environment* ev)
  57. {
  58.  
  59.     StandardFileReply sfReply;
  60.  
  61.     SFTypeList sfTypes;
  62.  
  63.     ODStorageUnit* mySU = this->GetStorageUnit(ev);
  64.  
  65.     ODDraft* myDraft = mySU->GetDraft(ev);
  66.  
  67.     ODSession* session = mySU->GetSession(ev);
  68.  
  69.     ODWindowState* windowState = session->GetWindowState(ev);
  70.  
  71.     ODContainer* insertContainer = kODNULL;
  72.  
  73.     
  74.     // Prompt the User to Insert… a file
  75.  
  76.     windowState->DeactivateFrontWindows(ev);
  77.  
  78.     StandardGetFile((FileFilterUPP)kODNULL,-1,sfTypes,&sfReply);
  79.  
  80.     windowState->ActivateFrontWindows(ev);
  81.  
  82.     
  83.     // If the user selected a file and clicked 'OK'
  84.  
  85.     if (sfReply.sfGood) 
  86.  
  87.     {
  88.  
  89.         ODFile* file = new ODFile();
  90.  
  91.         file->Specify(&(sfReply.sfFile));
  92.  
  93.         
  94.         TRY
  95.  
  96.             insertContainer = session->GetStorageSystem(ev)->
  97.  
  98.                 GetContainer(ev,kODDefaultFileContainer,file);
  99.  
  100.             // $$$$$ will become: file->GetContainerType(); //tç
  101.  
  102.         CATCH_ALL
  103.  
  104.             insertContainer = kODNULL;
  105.  
  106.         ENDTRY
  107.  
  108.     
  109.         // If the user chose an OpenDoc ODContainer
  110.  
  111.         if (insertContainer != kODNULL) 
  112.  
  113.         {
  114.  
  115.             ODDraft* nextDraft = kODNULL;
  116.  
  117.             ODDraft* insertDraft = kODNULL;
  118.  
  119.     
  120.             ODDocument* insertDocument =
  121.  
  122.                                         insertContainer->GetDocument(ev, kODDefaultDocument);
  123.  
  124.             insertDraft = insertDocument->GetBaseDraft(ev, kDPReadOnly);
  125.  
  126.             while (insertDocument->Exists(ev,0,insertDraft,kODPosLastAbove))
  127.  
  128.                                         insertDraft = aDocument->GetDraft(ev, kDPReadOnly,
  129.  
  130.                                                                  kODNULL, insertDraft, kODPosLastAbove,kODTrue);
  131.  
  132.             ODStorageUnitRef                suref;
  133.  
  134.             ODStorageUnit* draftSU = insertDraft->GetDraftProperties(ev);
  135.  
  136.             draftSU->Focus(ev, kODPropRootPartSU, kODPosUndefined,
  137.  
  138.                                                                     kODStorageUnit, 0, kODPosUndefined);
  139.  
  140.             draftSU->GetValue(ev, sizeof(ODStorageUnitRef), &suRef);
  141.  
  142.             ODStorageUnit* insertSU = insertDraft->GetStorageUnit(ev, 
  143.  
  144.                     draftSU->GetIDFromStorageUnitRef(ev, suRef));
  145.  
  146.     
  147.             // Clone the root part from the insertDraft to this draft.
  148.  
  149.             // This is identical to pasting a part from the Clipboard
  150.  
  151.             // Please refer to the Clipboard recipe to implement the
  152.  
  153.             // ClonePartAndEmbed function.
  154.  
  155.             this->ClonePartAndEmbed(ev, insertSU);
  156.  
  157.  
  158.             // Important: you must perform the following Release() calls
  159.  
  160.             // in the exact order they are listed.
  161.  
  162.             insertSU->Release(ev);
  163.  
  164.             insertDraft->Release(ev);
  165.  
  166.             insertDocument->Release(ev);
  167.  
  168.             insertContainer->Release(ev);
  169.  
  170.         }
  171.  
  172.         delete file;
  173.  
  174.     }
  175.  
  176. }